package com.leadership.api.service.impl;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.service.SearchIdmRequestService;
import com.leadership.api.service.SearchIdmService;
import com.leadership.entity.idm.IdmOrg;
import com.leadership.entity.idm.IdmUser;
import com.leadership.entity.idm.WsUser;

// import net.sf.json.JSONObject;


/**
 * 
 * @author LZJ
 *
 */
@Service("searchIdmService")
public class SearchIdmServiceImpl implements SearchIdmService {
    @Autowired
    @Qualifier("searchIdmRequestService")
    private SearchIdmRequestService searchIdmRequestService;

    // private static final String orgUrl
    // ="http://idmtest.cifi.com.cn:28080/sim/api/v2/org?filter=FILTER";

    // private static final String userUrl =
    // "http://idmtest.cifi.com.cn:28080/sim/api/v2/user?filter=FILTER&pageSize=1000000";
    private static final String orgUrl = "http://sim.cifi.com.cn/sim/api/v2/org?filter=FILTER";

    private static final String userUrl =
            "http://sim.cifi.com.cn/sim/api/v2/user?filter=FILTER&pageSize=1000000";

    private static final Logger logger = LoggerFactory.getLogger(SearchIdmServiceImpl.class);

    // 查询人员信息
    @SuppressWarnings("unchecked")
    public List<IdmUser> QueryUser(String filter) {

        List<IdmUser> idmUsers = new ArrayList<IdmUser>();
        try {
            filter = URLEncoder.encode(filter, "UTF-8");
            String url = userUrl.replace("FILTER", filter);
            logger.info("url is [{}]", url);
            String response = searchIdmRequestService.doGet(url, null);
            
            Map<String, Object> result = JsonUtils.parse(response, Map.class);
            //logger.info("# search response [{}] result [{}]  ", response, result);
            String code = (String) result.get("success");
            if (StringUtils.equalsIgnoreCase(code, "true")) {
                Map<String, Object> body = (Map<String, Object>) result.get("body");

                if (body != null && !body.isEmpty()) {
                    
                    Object object = body.get("wsUsers");
                    
                    if(object != null) {
                        String wsUserObject = JsonUtils.toJson(object);
                        List<WsUser> wsUsers = JsonUtils.parseAsList(wsUserObject, WsUser.class);
                        for (WsUser wsUser : wsUsers) {
                            IdmUser idmUser = new IdmUser(wsUser.getAttrs());
                            idmUsers.add(idmUser);
                        }
                    }
                    
                }
            } 
        } catch (Exception e) {
            logger.error("#QueryUser - call idm api failed, ", e);
        }
        
        return idmUsers;

    }

    // 查询组织信息
    @SuppressWarnings("unchecked")
    public List<IdmOrg> QueryOrg(String filter) {

        List<IdmOrg> idmOrgs = new ArrayList<IdmOrg>();
        String url = orgUrl.replace("FILTER", filter);
        
        String response = searchIdmRequestService.doGet(url, null);
        Map<String, Object> result = JsonUtils.parse(response, Map.class);
        String code = (String) result.get("success");
        if (StringUtils.equalsIgnoreCase(code, "true")) {
            String body = JsonUtils.toJson(result.get("body"));
            String wsUserObject = body.substring(1, body.length() - 1);

            if (StringUtils.isNotBlank(body)) {

                List<WsUser> wsUsers = JsonUtils.parseAsList(wsUserObject, WsUser.class);
                for (WsUser wsUser : wsUsers) {
                    IdmOrg idmOrg = new IdmOrg(wsUser.getAttrs());
                    idmOrgs.add(idmOrg);
                }
            }
        }
        return idmOrgs;
    }



}
